package com.woniu.airent.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.airent.dto.*;
import com.woniu.airent.service.ArticleService;
import com.woniu.airent.utils.TXYunUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;

@Controller
@RequestMapping("article")
public class ArticleController {
    @Autowired
    ArticleService articleService;

    //    添加(发帖)
    @RequestMapping("/postArticle")
    @ResponseBody
    public String addArticle(HttpServletRequest request, @RequestBody ArticleDto article) {
        UserDto user = (UserDto) request.getSession().getAttribute("user");
        article.setUserId(user.getUserId());

        article.setArticleTime(new Date());
        article.setArticleState(1);
        System.out.println(article);

//        添加
        int i = articleService.addArticle(article);

        return "success";


    }

    //    条件查询
    @RequestMapping("/query")
    @ResponseBody
    public PageInfo<ArticleDto> queryArticle(@RequestParam(defaultValue = "1") int pageNum,
                                             @RequestParam(defaultValue = "2") int pageSize,
                                             String keyWord, int status) {
        PageInfo<ArticleDto> pageInfo = articleService.query(pageNum, pageSize, keyWord, status);
        System.out.println(pageInfo);
        return pageInfo;
    }

    //单个帖子查询
    @RequestMapping("/singleQuery")
    @ResponseBody
    public ArticleDto singleQuery(int id) {
        ArticleDto articleDto = articleService.singleQuery(id);
        System.out.println(articleDto);
        return articleDto;
    }

    //    回帖
    @RequestMapping("/reply")
    @ResponseBody
    public String replyArticle(HttpServletRequest request, @RequestBody ReplyDto replyDto) {
        UserDto user = (UserDto) request.getSession().getAttribute("user");
        replyDto.setFromId(user.getUserId());
        replyDto.setFromName(user.getUserName());
        replyDto.setTime(new Date());
        System.out.println(replyDto);
        int i = articleService.addReply(replyDto);
        return i != 0 ? "success" : "error";

    }

    @PostMapping(value = "/addImg")
    @ResponseBody
    public Map<String, Object> upload(@RequestParam("files") MultipartFile multipartFile, Model model) throws Exception {
        //获取文件的名称
        String fileName = multipartFile.getOriginalFilename();

        //获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));

        //使用uuid作为文件名，防止生成的临时文件重复
        final File excelFile = File.createTempFile("imagesFile-" + System.currentTimeMillis(), prefix);
        //将Multifile转换成File
        multipartFile.transferTo(excelFile);

        //调用腾讯云工具上传文件
        String imageName = TXYunUtil.uploadfile(excelFile, "avatar");

        //程序结束时，删除临时文件
        deleteFile(excelFile);

        //存入图片名称，用于网页显示
//        model.addAttribute("imageName", imageName);

        Map<String, Object> map = new HashMap<>();
        List<String> data = new ArrayList<>();
        data.add("https://airent-1303816068.cos.ap-shanghai.myqcloud.com/" + imageName);
        map.put("errno", 0);
        /*map.put("data",data);*/
        map.put("url", imageName);
        System.out.println(map);

        //返回成功信息
        return map;
    }

    private void deleteFile(File... files) {
        for (File file : files) {
            if (file.exists()) {
                file.delete();
            }
        }
    }

    @RequestMapping("/delImg")
    public void delImg(String[] imgs) {
        for (int i = 0; i < imgs.length; i++) {
            TXYunUtil.deletefile(imgs[i]);
        }
    }


    @RequestMapping("/reported")
    @ResponseBody
    public String reported(ArticleReportDto articleReport) {
        System.out.println(articleReport);
        articleReport.setReportDate(new Date());

        int i = articleService.addReport(articleReport);

        return i != 0 ? "success" : "error";
    }


    @RequestMapping("/showReported")
    @ResponseBody
    public List<ArticleReportDto> showReported(int articleId) {
        List<ArticleReportDto> pageInfo = articleService.showReported(articleId);
        return pageInfo;
    }

    @RequestMapping("/showOneUserReported")
    @ResponseBody
    public PageInfo<ReplyDto> showOneUserReported(HttpServletRequest request,String keyWord) {
//        UserDto user = (UserDto) request.getSession().getAttribute("user");
//        PageInfo<ReportDto> pageInfo = articleService.showOneUserReported(user.getUserId());
        PageInfo<ReplyDto> pageInfo = getReply(request,keyWord);
        return pageInfo;
    }

    @RequestMapping("/delReply")
    @ResponseBody
    public PageInfo<ReplyDto> delReply(HttpServletRequest request, int replyId) {
        int result = articleService.delReply(replyId);
        if (replyId!=0){
            PageInfo<ReplyDto> pageInfo = getReply(request,null);
            return pageInfo;
        }
        return null;
    }

    public PageInfo<ReplyDto> getReply(HttpServletRequest request,String keyWord) {
        UserDto user = (UserDto) request.getSession().getAttribute("user");
        PageInfo<ReplyDto> pageInfo = articleService.showOneUserReported(user.getUserId(),keyWord);
        return pageInfo;
    }

}
